﻿using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;

namespace SharePointContentGenerator.ContentGeneratorLogic.ContentGenerators
{
    internal class WordDocumentGenerator
    {
        internal const string WORDDOC = "<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:body><w:p><w:r><w:t>Create text in body - CreateWordprocessingDocument</w:t></w:r></w:p></w:body></w:document>";

        internal void CreateWordDocument(string documentPath)
        {
            // Create a document by supplying the filepath. 
            using (WordprocessingDocument wordDocument =
                WordprocessingDocument.Create(documentPath, WordprocessingDocumentType.Document))
            {
                // Add a main document part. 
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

                // Create the document structure and add some text.
                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());
                run.AppendChild(new Text("Create text in body - CreateWordprocessingDocument"));
            }
        }

        internal MemoryStream CreateWordDocument()
        {
            MemoryStream ms = new MemoryStream();

            using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document, true))
            {
                // Add a main document part. 
                MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();

                // Create the document structure and add some text.
                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());

                // This would allow word to be open and generate randomly 10 pragraphs of 20 lines
                // run.AppendChild(new Text("=Rand(10, 20)"));
                // However, using random Lipsum content, from: http://code.google.com/p/nlipsum/
                // Assembly is provided
                string randomContent = GetRandomContent();
                run.AppendChild(new Text(randomContent));

                wordDoc.MainDocumentPart.Document.Save();

                wordDoc.Close();
            }

            return ms;
        }

        internal string GetRandomContent()
        {
            string randomContent = string.Empty;

            string randomLipsum = GetRandomLipsum();
            randomContent = NLipsum.Core.LipsumGenerator.Generate(10, randomLipsum);

            return randomContent;
        }

        internal string GetRandomLipsum()
        {
            string s = string.Empty;
            Random rnd = new Random();
            switch(rnd.Next(0, 11))
            {
                case 0: s = NLipsum.Core.Lipsums.ChildHarold; break;
                case 1: s = NLipsum.Core.Lipsums.Decameron; break;
                case 2: s = NLipsum.Core.Lipsums.Faust; break;
                case 3: s = NLipsum.Core.Lipsums.InDerFremde; break;
                case 4: s = NLipsum.Core.Lipsums.LeBateauIvre; break;
                case 5: s = NLipsum.Core.Lipsums.LeMasque; break;
                case 6: s = NLipsum.Core.Lipsums.LoremIpsum; break;
                case 7: s = NLipsum.Core.Lipsums.NagyonFaj; break;
                case 8: s = NLipsum.Core.Lipsums.Omagyar; break;
                case 9: s = NLipsum.Core.Lipsums.RobinsonoKruso; break;
                case 10: s = NLipsum.Core.Lipsums.TheRaven; break;
                case 11: s = NLipsum.Core.Lipsums.TierrayLuna; break;
                default: s = NLipsum.Core.Lipsums.LoremIpsum; break;
            }
            return s;
        }
    }
}
